define(function (require, exports, module) {
    var angular = require('angular');
    var $ = require('jquery');
    var util = require('util');
    require("angular-ui"); 
    require("authorize");
    require("permission");
    require("angular-interceptor");
    require("pagination");

    angular.module('app', ['ngInterceptor', 'ngUI'])
        .constant('api.path', "/api/index.php?r=init/")
        .factory('ErpApiService', [
            '$http',
            'api.path',
            function ($http, api_path) {
                var httpClient = {
                    get: function (api, data) {
                        var url = api_path + api;
                        var search = (data && data.search) || {};
                        angular.forEach(search, function (val, key) {
                            url += "&" + key + "=" + val;
                        });

                        return $http.get(url)
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode == "0") {
                                    return res;
                                }
                            })
                    },
                    post: function (api, data) {
                        return $http.post(api_path + api, (data && data.data) || {})
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode == "0") {
                                    return res;
                                }
                            })
                    }
                }
                return {
                    getApiList: function (search) {
                        return httpClient.get('interface-config/get-erp-api-list', search)
                    },
                    execApi: function (search) {
                        return httpClient.get('interface-config/exec-erp-api', search)
                    }
                }
            }
        ])
        .controller('ErpApiController', [
            '$scope',
            '$rootScope',
            'ErpApiService',
            function ($scope, $rootScope, service) {

               $scope.execApi = function(){
                   service.execApi({
                       search: {
                           app_code: "5002"
                       }
                   })
                       .then(function(res){
                           $rootScope.$emit('httpRequestSuccess',"执行成功");
                           $scope.getApiList({
                               page_index: 1,
                               page_size: $scope.pager.pageSize
                           });
                       })
               };
               
               $scope.getApiList = function(opts){
                   opts = opts || {
                       page_index: 1,
                       page_size: 10
                   };
                   
                   return service.getApiList({
                       search: angular.extend(opts,{
                           app_code: "5002"
                       })
                   })
                       .then(function(res){
                           $scope.apiList = res.data.info;
                           $scope.pager.total = res.data.total;
                           $scope.pager.pageIndex = opts.page_index;
                       })
               };
               
               $scope.pager = {
                   pageIndex: 1,
                   pageSize: 10,
                   change: function(pageIndex) {
                       var data = {
                           page_index: pageIndex,
                           page_size: $scope.pager.pageSize
                       };
                       
                       if ($scope.field) {
                           data = angular.extend(data,{
                                sort_field: $scope.field,
                                sort_order: $scope[$scope.field].desc ? "desc" : "asc"
                           })
                       }
                       $scope.getApiList(data);
                   }
               };
               
               $scope.sort = function(field) {
                   $scope[field] = $scope[field] || {};
                   $scope[field].desc = !$scope[field].desc;
                   $scope.field = field;
                   $scope.getApiList({
                        page_index: 1,
                        page_size: $scope.pager.pageSize,
                        sort_field: field,
                        sort_order: $scope[field].desc ? "desc" : "asc"
                    })
                        .then(function(){
                            
                            //$scope[field].desc = !$scope[field].desc;
                        })
               };
               
               $scope.getApiList();
               
            }
        ])
		
    angular.element(document).ready(function () {
        angular.bootstrap(document, ['app']);
    });
});